package net.atomarrow.bs.controller;

import net.atomarrow.annotation.Url;
import net.atomarrow.configs.DbConfig;
import net.atomarrow.db.orm.OrmContext;
import net.atomarrow.domains.DbMaker;
import net.atomarrow.domains.DomainUtil;
import net.atomarrow.ioc.ApplicationContext;
import net.atomarrow.render.Render;
import net.atomarrow.services.IService;
import net.atomarrow.services.Service;
import net.atomarrow.sy.base.BaseController;

import java.util.List;

public class IndexController extends BaseController {
	@Url("/updateTable")
	public Render updateTable(){
	    ApplicationContext.load(new Service());
	    IService service = ApplicationContext.get(Service.class);
	    DbConfig config = ApplicationContext.get(DbConfig.class);
	    DbMaker dm = new DbMaker(config.getDatabaseName());
	    List<String> domainNames = OrmContext.getSingleTableDomainNames();
	    for(String domainName:domainNames){
	        dm.makeTable(domainName,OrmContext.getDomainClass(domainName), service);
	    }
	    List<String> domainNames2 = OrmContext.getMultipleTableDomainNames();
        for(String domainName:domainNames2){
            dm.makeTable(DomainUtil.getMultiTableName(domainName, 1),OrmContext.getDomainClass(domainName), service);
        }
	    return renderJsonSuccess();
	}

}
